package com.example.day01_weather.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.day01_weather.Img;
import com.example.day01_weather.R;
import com.example.day01_weather.bean.WeatherBean;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 *
 */
public class WeatherAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private WeatherBean.ResultBean dataBean;
    private ArrayList<WeatherBean.ResultBean.DailyBean> daily;
    private Context context;
    private int VIEW_ONE_TYPE = 0;
    private int VIEW_TWO_TYPE = 1;
    private final LayoutInflater inflater;
    private String city;
    public WeatherAdapter( ArrayList<WeatherBean.ResultBean.DailyBean> daily, Context context,String city) {
        this.daily = daily;
        this.context = context;
        this.city=city;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return VIEW_ONE_TYPE;
        } else {
            return VIEW_TWO_TYPE;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == VIEW_ONE_TYPE) {
            View view = inflater.inflate(R.layout.item_today, parent, false);
            return new ViewHolderOne(view);
        } else {
            View view = inflater.inflate(R.layout.item_day, parent, false);
            return new ViewHolderTwo(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolderOne) {

            int image = new Img().Image(daily.get(position).getDay().getImg());
            Glide.with(context).load(image).into(((ViewHolderOne) holder).ivWeek);
            String city = daily.get(position).getDay().getWeather();
            ((ViewHolderOne) holder).tvCalendar.setText(city);
            ((ViewHolderOne) holder).tvWinddirect.setText(this.city);
        } else if (holder instanceof ViewHolderTwo) {
            ((ViewHolderTwo) holder).tvDay.setText(daily.get(position).getDate());
            int image = new Img().Image(daily.get(position).getDay().getImg());
            Glide.with(context).load(image).into(((ViewHolderTwo) holder).ivItem);
            
        }
    }

    @Override
    public int getItemCount() {
        return daily.size();
    }

    static
    class ViewHolderOne extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_date)
        TextView tvDate;
        @BindView(R.id.tv_week)
        TextView tvWeek;
        @BindView(R.id.tv_calendar)
        TextView tvCalendar;
        @BindView(R.id.iv_week)
        ImageView ivWeek;
        @BindView(R.id.tv_weather)
        TextView tvWeather;
        @BindView(R.id.tv_temp)
        TextView tvTemp;
        @BindView(R.id.tv_winddirect)
        TextView tvWinddirect;

        ViewHolderOne(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


    static
    class ViewHolderTwo extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.iv_item)
        ImageView ivItem;
        @BindView(R.id.tv_day)
        TextView tvDay;
        @BindView(R.id.tv_c)
        TextView tvC;

        ViewHolderTwo(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
